<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="项目号" prop="projectNumber">
        <el-input v-model="formData.projectNumber" placeholder="请输入项目号" />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="formData.companyName" placeholder="请输入公司名称" />
      </el-form-item>
      <el-form-item label="项目类型：1新注册 2代理记账 3迁移" prop="projectType">
        <el-checkbox-group v-model="formData.projectType">
          <el-checkbox
            v-for="dict in getStrDictOptions(DICT_TYPE.BPM_PROJECT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="注册园区及平台" prop="registrationPark">
        <el-input v-model="formData.registrationPark" placeholder="请输入注册园区及平台" />
      </el-form-item>
      <el-form-item label="法人" prop="legalPerson">
        <el-input v-model="formData.legalPerson" placeholder="请输入法人" />
      </el-form-item>
      <el-form-item label="法人实名电话号码" prop="legalPersonPhone">
        <el-input v-model="formData.legalPersonPhone" placeholder="请输入法人实名电话号码" />
      </el-form-item>
      <el-form-item label="企业联系人" prop="enterpriseContact">
        <el-input v-model="formData.enterpriseContact" placeholder="请输入企业联系人" />
      </el-form-item>
      <el-form-item label="企业联系人电话" prop="enterpriseContactPhone">
        <el-input v-model="formData.enterpriseContactPhone" placeholder="请输入企业联系人电话" />
      </el-form-item>
      <el-form-item label="纳税人属性" prop="taxpayerAttribute">
        <el-input v-model="formData.taxpayerAttribute" placeholder="请输入纳税人属性" />
      </el-form-item>
      <el-form-item label="收费" prop="fee">
        <el-input v-model="formData.fee" placeholder="请输入收费" />
      </el-form-item>
      <el-form-item label="招商负责人" prop="investmentLeader">
        <el-input v-model="formData.investmentLeader" placeholder="请输入招商负责人" />
      </el-form-item>
      <el-form-item label="介绍人或下级平台" prop="introducer">
        <el-input v-model="formData.introducer" placeholder="请输入介绍人或下级平台" />
      </el-form-item>
      <el-form-item label="税收情况" prop="taxReformSituation">
        <el-input v-model="formData.taxReformSituation" placeholder="请输入税收情况" />
      </el-form-item>
      <el-form-item label="是否为简化企业" prop="isSimplifiedEnterprise">
        <el-radio-group v-model="formData.isSimplifiedEnterprise">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否为重点企业" prop="isKeyEnterprises">
        <el-radio-group v-model="formData.isKeyEnterprises">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="返税比例" prop="taxRebateRatio">
        <el-input v-model="formData.taxRebateRatio" placeholder="请输入返税比例" />
      </el-form-item>
      <el-form-item label="项目引进情况说明" prop="projectIntroduction">
        <el-input v-model="formData.projectIntroduction" placeholder="请输入项目引进情况说明" />
      </el-form-item>
      <el-form-item label="审批结果" prop="status">
        <el-select v-model="formData.status" placeholder="请选择审批结果">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="流程实例的编号" prop="processInstanceId">
        <el-input v-model="formData.processInstanceId" placeholder="请输入流程实例的编号" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { BusinessProjectApi, BusinessProjectVO } from '@/api/bpm/businessproject'

/** 引入项目审核信息 表单 */
defineOptions({ name: 'BusinessProjectForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  projectNumber: undefined,
  companyName: undefined,
  projectType: [],
  registrationPark: undefined,
  legalPerson: undefined,
  legalPersonPhone: undefined,
  enterpriseContact: undefined,
  enterpriseContactPhone: undefined,
  taxpayerAttribute: undefined,
  fee: undefined,
  investmentLeader: undefined,
  introducer: undefined,
  taxReformSituation: undefined,
  isSimplifiedEnterprise: undefined,
  isKeyEnterprises: undefined,
  taxRebateRatio: undefined,
  projectIntroduction: undefined,
  status: undefined,
  processInstanceId: undefined,
})
const formRules = reactive({
  projectNumber: [{ required: true, message: '项目号不能为空', trigger: 'blur' }],
  companyName: [{ required: true, message: '公司名称不能为空', trigger: 'blur' }],
  projectType: [{ required: true, message: '项目类型：1新注册 2代理记账 3迁移不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '审批结果不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BusinessProjectApi.getBusinessProject(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BusinessProjectVO
    if (formType.value === 'create') {
      await BusinessProjectApi.createBusinessProject(data)
      message.success(t('common.createSuccess'))
    } else {
      await BusinessProjectApi.updateBusinessProject(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    projectNumber: undefined,
    companyName: undefined,
    projectType: [],
    registrationPark: undefined,
    legalPerson: undefined,
    legalPersonPhone: undefined,
    enterpriseContact: undefined,
    enterpriseContactPhone: undefined,
    taxpayerAttribute: undefined,
    fee: undefined,
    investmentLeader: undefined,
    introducer: undefined,
    taxReformSituation: undefined,
    isSimplifiedEnterprise: undefined,
    isKeyEnterprises: undefined,
    taxRebateRatio: undefined,
    projectIntroduction: undefined,
    status: undefined,
    processInstanceId: undefined,
  }
  formRef.value?.resetFields()
}
</script>